package com.bdqn.oa.controller;

import com.bdqn.oa.entity.Attendance;
import com.bdqn.oa.service.AttendanceService;
import com.bdqn.oa.service.impl.AttendanceServiceImpl;
import com.bdqn.oa.utils.DateUtils;
import com.bdqn.oa.utils.Page;
import com.bdqn.oa.utils.ResultCode;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;

/**
 * @author zhowei
 * @date 2022/7/20 15:19
 */
public class AttendanceController extends HttpServlet {
private AttendanceService attendanceService =  new AttendanceServiceImpl();

    /**
     * 上班打卡
     * @param req
     * @param resp   （测试通过）
     * @return
     * @throws Exception
     */   //http://localhost:8081/do/attendance/insertMorningAtt?employee_id=2&type=1
    public ResultCode insertMorningAtt(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        Integer employeeId=Integer.valueOf(req.getParameter("employee_id"));
        if(employeeId == null){
            return ResultCode.failed("上班打卡失败!");
        }
        Integer type=Integer.valueOf(req.getParameter("type"));
        Attendance attendance = new Attendance();
        attendance.setEmployee_Id(employeeId);
        attendance.setStart_Time(LocalDateTime.now());
        attendance.setType(type);
        attendance.setEnd_Time(null);
        attendance.setTotal_Time(0.0);
        if(attendanceService.insertMorningAtt(attendance)==1){
            return ResultCode.success("上班打卡成功!");
        }else{
            //resp.sendRedirect("/index.jsp");   // 重定向到打卡界面
            return ResultCode.failed("上班打卡失败!");
        }
    }

    /**
     * 下班打卡
     * @param req
     * @param resp  (测试通过)
     * @return
     * @throws Exception
     */  //http://localhost:8081/do/attendance/insertAfternoonTimeAtt?employee_id=2&totalTime=1.0
    public ResultCode insertAfternoonTimeAtt(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        Integer employeeId=Integer.valueOf(req.getParameter("employee_id"));
        Double totalTime=Double.valueOf(req.getParameter("totalTime"));
      //  Integer type=Integer.valueOf(req.getParameter("type"));
        if(employeeId == null){
            return ResultCode.failed("下班打卡失败!");
        }
        Attendance attendance = attendanceService.getAttendByEidByDate(employeeId, DateUtils.getLocalDateTime());
        //attendance.setType(type);
        attendance.setTotal_Time(totalTime);
        attendance.setEnd_Time(LocalDateTime.now());
        if(attendanceService.insertAfternoonTimeAtt(attendance)==1){
            return ResultCode.success("下班打卡成功!");
        }else{
//            resp.sendRedirect("/index.jsp");   // 重定向到打卡界面
            return ResultCode.failed("下班打卡失败!");
        }
    }

    /**
     * 查看某员工所有打卡记录
     * @param req
     * @param resp  （测试通过）
     * @return
     * @throws Exception
     */  //http://localhost:8081/do/attendance/getAttendByEidOfPage?employee_id=2
    public ResultCode getAttendByEidOfPage(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        Integer employeeId=Integer.valueOf(req.getParameter("employee_id"));
        long pageNumber=req.getParameter("pageNumber")==null?1:Long.parseLong(req.getParameter("pageNumber"));
        long pageSize=req.getParameter("pageSize")==null?1:Long.parseLong(req.getParameter("pageSize"));
        Page<Attendance> attendancePage = attendanceService.getAttendByEidOfPage(employeeId,pageNumber,pageSize);
        attendancePage.setCount(attendanceService.countById(employeeId));
        return ResultCode.success(attendancePage);
    }
}
